package com.hunk.core.excel;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hunk.utils.CommonUtils;
import com.hunk.utils.StringHelper;
import com.hunk.utils.io.Print;

/**
 * @author HunkDeng
 * @since 2013-1-6
 */
class ImportCrop implements PoiCallback<String> {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PoiService poi = new PoiService();
		List<String> crop_names = null;
		try {
			crop_names = poi.read(new ImportCrop());
//			Print.println(crop_names);
		} catch (VersionInCorrectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Print.println(crop_names.size());
		int i = 4;
		for (String crop_name : crop_names) {
			String insert_sql = "insert into centerdb_js.dbo.bzy_crop " +
					"(crop_ID, crop_code, name) " + 
					"values " +  
					"('?','?','?');";
			
			String uuid = CommonUtils.generateUUID();
			String crop_code = StringHelper.numberFormatWithZeroBegin(i,3);
			
			insert_sql = insert_sql.replaceFirst("[?]", uuid);
			insert_sql = insert_sql.replaceFirst("[?]", crop_code);
			insert_sql = insert_sql.replaceFirst("[?]", crop_name);
			i++;
			
			Print.println(insert_sql);
		}
	}

	private Set<String> crop_nameSet = new HashSet<String>();
	
	@Override
	public String parse(HSSFRow hssfRow, HSSFWorkbook hssfWorkBook,
			HSSFSheet hssfSheet) {
		if (!"汇总表".equals(hssfSheet.getSheetName())) return null;
		if (6 > hssfRow.getRowNum()) return null;
		String crop_name;
		crop_name = hssfRow.getCell(1).getStringCellValue();
		if (crop_nameSet.add(StringHelper.trim(crop_name)))
			return StringHelper.trim(crop_name);
		else 
			return null;
	}

	@Override
	public String getPath() {
		return "E:/HunkDeng/ProjectDoc/金山/文档/20121209start/20121220/蔬菜品种.xls";
	}

	@Override
	public void check(HSSFWorkbook hssfWorkbook)
			throws VersionInCorrectException {
		// TODO Auto-generated method stub
		
	}

}
